package com.pinecone.service.impl;

import com.pinecone.common.Constant.RoleConstant;
import com.pinecone.common.exception.MinerException;
import com.pinecone.dao.CusPrincipalDao;
import com.pinecone.dao.SysRoleDao;
import com.pinecone.dao.SysUserDao;
import com.pinecone.dao.SysUserRoleDao;
import com.pinecone.dto.UserDetailsDTO;
import com.pinecone.entity.CusPrincipalEntity;
import com.pinecone.entity.SysRoleEntity;
import com.pinecone.entity.SysUserEntity;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2017/8/26.
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private CusPrincipalDao cusPrincipalDao;
    @Override
    public UserDetailsDTO loadUserByUsername(String userAndRole) {
        //通过userAndRole区分当前用户是系统管理员还是客户
        String username = userAndRole.substring(0,userAndRole.indexOf(":"));
        String rolename = userAndRole.substring(userAndRole.indexOf(":")+1,userAndRole.length());
        UserDetailsDTO userDetails = new UserDetailsDTO();
        //机构客户用户
        if (rolename.contains("ORG")){

            CusPrincipalEntity cusPrincipal = cusPrincipalDao.queryByAccount(username);
            if (cusPrincipal != null){
                userDetails.setUsername(cusPrincipal.getCusAccount())
                        .setEnabled(cusPrincipal.isState())
                        .setLastPasswordResetDate(cusPrincipal.getLastPasswordResetDate())
                        .setPassword(cusPrincipal.getCusPassword())
                        .setId(cusPrincipal.getCusId())
                        .setOrgId(cusPrincipal.getOrgId());
                //为客户设置权限和角色
                Set<GrantedAuthority> authorities = new HashSet<>();
                SimpleGrantedAuthority orgMaster = new SimpleGrantedAuthority(RoleConstant.ORGMASTER);
                SimpleGrantedAuthority orgHost = new SimpleGrantedAuthority(RoleConstant.ORGHOST);
                if (cusPrincipal.isParent()){
                    //管理员
                    userDetails.setRolename(RoleConstant.ORGMASTER);
                    authorities.add(orgMaster);
                    //authorities.add(orgHost);
                }
                if (!cusPrincipal.isParent()){
                    //主播
                    userDetails.setRolename(RoleConstant.ORGHOST);
                    authorities.add(orgHost);
                }
                userDetails.setAuthorities(authorities);
            }
        }
        //系统用户
        if (rolename.contains("SYS")){
            SysUserEntity userEntity = sysUserDao.queryByUserName(username);
            if (userEntity != null){
                userDetails.setId(userEntity.getUserId())
                        .setEnabled(userEntity.isEnable())
                        .setLastPasswordResetDate(userEntity.getLastPasswordResetDate())
                        .setPassword(userEntity.getPassword())
                        .setUsername(userEntity.getUsername());
                //设置权限和角色
                Set<GrantedAuthority> authorities = new HashSet<>();
                SysRoleEntity roleEntity = sysRoleDao.queryRolesByUserId(userEntity.getUserId());
                if (roleEntity == null){
                    throw new MinerException("该用户未授予任何角色");
                }
                userDetails.setRolename(roleEntity.getRoleName());
                authorities.add(roleEntity);
                userDetails.setAuthorities(authorities);
            }
        }
        return userDetails;
    }
}
